#ifndef __COMMANDLINESTREAM_H__
#define __COMMANDLINESTREAM_H__

#include "plat/nonecopyable.h"
#include "lib/cqueue.h"
#include "os/msgqueue.h"
#include "os/thread.h"
#include "commandlinedef.h"
#include <vector>
#include "lib/cqueue.h"

class CommandLineMgrBase;

class CommandLineStream : public NoneCopyable
{
public:
    explicit CommandLineStream();
    virtual ~CommandLineStream();

public:
    void AttacthCommandMgr(CommandLineMgrBase *mgr);
    void DettachCommandMgr();

    void Start();
    void Stop();

    //接收到数据
    void AddRecvByte(uint8_t c);
    void SendTo(const char *data);
    void SendResp(const char *data);

protected:
    virtual bool OnWriteStream(const char *str) = 0;

private:
    bool Parse(const std::string &line, CommandParamItem &item);
    void HandleLine(const std::string &line);
    void HandleLoop();
    void HandleRecvByteParse(char preC, char c);
    void AddCmdToQueue(const std::string &cmd);

private:
    CommandLineMgrBase *m_commandMgr = nullptr;
    Thread m_thread{COMMANDLINE_THREAD_STACK_SIZE};
    MsgQueue<char, COMMANDLINE_RECV_MSGQ_MAX> m_recvQ;
    std::string m_line;

    bool m_fnRecvStart = false;
    uint32_t m_fnKeyCode = 0;
    int m_fnRecvCount = 0;

    int m_currentExecCmdIndex = -1;
    std::vector<std::string> m_historyCmds;
};

#endif // __COMMANDLINESTREAM_H__
